<template>
	<view>
		<view class="tb_imgs">
			<text>头像</text>
			<view class="img_right" @click="uploadImg">
				<image class="photo" :src="userInfo.avatarUrl"></image>
				<image class="right" src="../../static/first/right.png"></image>
			</view>
		</view>
		<!-- 昵称 -->
		<view class="tb_name">
			<text>昵称</text>
			<view>
				<input v-model="userInfo.nickName" />
			</view>
		</view>
		<view class="btn" @click="confirm">
			<button>保存</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					nickName: '',
					avatarUrl: '',
					id: ''
				}
			}
		},
		onLoad() {
			let userInfo = uni.getStorageSync('userInfo')
			this.userInfo.nickName = userInfo.nickName
			this.userInfo.avatarUrl = userInfo.avatarUrl
			this.userInfo.id = userInfo.id
		},
		methods: {
			uploadImg() {
				let that = this
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['camera', 'album'],
					success: function(res) {
						uni.uploadFile({
							url: that.$serverUrl + 'uploadUtil/uploadImage',
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (uploadRes) => {
								let data = JSON.parse(uploadRes.data)
								if (data.success) {
									that.userInfo.avatarUrl = data.result
								} else {
									uni.showToast({
										title: '头像上传失败',
										icon: 'none'
									})
								}
							}
						})
					}
				})
			},
			confirm() {
				uni.request({
					url: this.$serverUrl + 'ftUsers',
					data: {
						"avatarUrl": this.userInfo.avatarUrl,
						"nickName": this.userInfo.nickName,
						"id": this.userInfo.id
					},
					method: 'PUT',
					success: (res) => {
						if (res.data.success) {
							let userInfo = uni.getStorageSync('userInfo')
							userInfo.avatarUrl = this.userInfo.avatarUrl
							userInfo.nickName = this.userInfo.nickName
							uni.setStorageSync('userInfo', userInfo)
							uni.showToast({
								title: '修改成功',
								icon: 'none'
							})
						} else {
							uni.showToast({
								title: '修改失败',
								icon: 'none'
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
	.tb_imgs {
		height: 142upx;
		background-color: #FFFFFF;
		border-bottom: 1px solid #C0C0C0;
		margin: 0 36upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.img_right {
		display: flex;
		align-items: center;
	}

	.photo {
		width: 88upx;
		height: 88upx;
		border-radius: 50%;
	}

	.right {
		width: 28upx;
		height: 28upx;
		margin-left: 16upx;
	}

	.tb_name {
		height: 142upx;
		background-color: #FFFFFF;
		border-bottom: 1px solid #C0C0C0;
		margin: 0 36upx;
		display: flex;
		align-items: center;
	}

	.tb_name view {
		margin-left: 124upx;
	}

	/* 添加按钮 */
	.btn {
		width: 100%;
		height: 124upx;
		position: fixed;
		bottom: 0;
		background-color: #FFFFFF;
	}

	.btn button {
		margin: 0upx 36upx;
		background-color: #00B2CA;
		color: #FFFFFF;
	}
</style>
